//
//  LZOrderPresenter.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/24.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZOrderPresenter.h"
#import "NetworkServiceManager.h"
#import "NSString+Additions.h"
#import "MJExtension.h"

#import "LZOrderModel.h"

extern NSString *merchant;
static NSString *const kTotalOrderListURL = @"/order/list";   //全部订单列表 包含已经结算,未结算,无需结算的列表
static NSString *const kFinancialOrderListURL = @"/settlement/unSettlementOrder";   //查询 已经结算,未结算列表

@implementation LZOrderPresenter

#pragma mark -
#pragma mark 订单列表
/**
 获取 订单列表接口
 
 @param sellerId 商户id
 @param cPage 当前页数
 @param limit 每页数量
 @param successBlcok <#successBlcok description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getOrderListWithSellerID:(NSString *)sellerId Page:(int)cPage Limit:(int)limit ResultSuccess:(void (^)(int, int, int, NSArray *))successBlcok Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kTotalOrderListURL);
    NSDictionary *parmeter = @{@"sellerId" : sellerId,
                               @"cpage" : [NSString stringWithFormat:@"%d",cPage],
                               @"limit" : [NSString stringWithFormat:@"%d",limit],
                               };
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:parmeter success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            int totalPageTemp = [resultData[@"data"][@"totalPages"] intValue];
            int totalCountTemp = [resultData[@"data"][@"totalCount"] intValue];
            int currentPageTemp = [resultData[@"data"][@"cPageNO"] intValue];
            NSMutableArray *modelArr = [[NSMutableArray alloc] init];
            NSArray *arrTemp = resultData[@"data"][@"list"];
            for (int i = 0; i < arrTemp.count; i ++) {
                LZOrderModel *m = [LZOrderModel mj_objectWithKeyValues:arrTemp[i]];
                [modelArr addObject:m];
            }
            
            if (successBlcok) {
                successBlcok(totalPageTemp, totalCountTemp,currentPageTemp, modelArr);
            }
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];
}


/**
 查询 已经结算  未结算订单列表
 
 @param sellerId 商户id
 @param pState 1 已经结算  0  未结算
 @param cPage <#cPage description#>
 @param limit <#limit description#>
 @param successBlcok <#successBlcok description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getFinancialOrderListWithSellerID:(NSString *)sellerId PayedState:(NSString *)pState Page:(int)cPage Limit:(int)limit ResultSuccess:(void (^)(int, int, int, NSArray *))successBlcok Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kFinancialOrderListURL);
    NSDictionary *parmeter = @{@"sellerId" : sellerId,
                               @"cpage" : [NSString stringWithFormat:@"%d",cPage],
                               @"limit" : [NSString stringWithFormat:@"%d",limit],
                               @"state" : pState
                               };
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:parmeter success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            int totalPageTemp = [resultData[@"data"][@"totalPages"] intValue];
            int totalCountTemp = [resultData[@"data"][@"totalCount"] intValue];
            int currentPageTemp = [resultData[@"data"][@"cPageNO"] intValue];
            NSMutableArray *modelArr = [[NSMutableArray alloc] init];
            NSArray *arrTemp = resultData[@"data"][@"list"];
            for (int i = 0; i < arrTemp.count; i ++) {
                LZOrderModel *m = [LZOrderModel mj_objectWithKeyValues:arrTemp[i]];
                [modelArr addObject:m];
            }
            
            if (successBlcok) {
                successBlcok(totalPageTemp, totalCountTemp,currentPageTemp, modelArr);
            }
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];

}
@end
